package com.halixun.d2app.dialog;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.halixun.d2app.R;
import com.halixun.d2app.utils.ToastUtils;


/**
 * A simple {@link Fragment} subclass.
 */
public class ScannerDialogFragment extends DialogFragment implements View.OnClickListener {

    private EditText number;

    private ScannerDialogListener listener;

    public ScannerDialogFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scanner_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners(view);
    }

    private void setListeners(View view) {
        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.confirm).setOnClickListener(this);
        number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String code = s.toString().trim().replace(" ", "");
                if (code.length()==18){
                    if (null != listener) {
                        listener.onScannerDialogClickListener(code);
                        number.setText("");
                        dismiss();
                    }
                }
            }
        });
    }

    private void initView(View view) {
        number = view.findViewById(R.id.number);
        number.setInputType(InputType.TYPE_NULL);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                String num = number.getText().toString().trim();
                if (TextUtils.isEmpty(num)) {
                    ToastUtils.showBottomToast("请输入付款码");
                    return;
                }
                if (null != listener) {
                    listener.onScannerDialogClickListener(num);
                }
            case R.id.cancel:
                number.setText("");
                dismiss();
                break;
        }
    }

    public void setScannerDialogListener(ScannerDialogListener listener) {
        this.listener = listener;
    }

    public interface ScannerDialogListener {
        void onScannerDialogClickListener(String number);
    }

}
